﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using APIShoppingSystem.Models.RoleInfoViewModel;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleInfoController : ControllerBase
    {
        private IRoleInfoBll _roleInfoBll;
        private IUserInfoBll _useInfoBll;
        private IMenuInfoBll _menuInfoBll;
        public RoleInfoController(IRoleInfoBll roleInfoBll
            , IUserInfoBll useInfoBll
            , IMenuInfoBll menuInfoBll)
        {
            _roleInfoBll = roleInfoBll;
            _useInfoBll = useInfoBll;
            _menuInfoBll = menuInfoBll;
        }
        /// <summary>
        /// /获取数据列表集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleInfoList(int page, int limit, string roleName, string phoneNum)
        {
            int count;

            object roleInfos = _roleInfoBll.GetRoleInfoByPage(page, limit, out count, roleName);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = roleInfos
            });
        }


        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRoleInfo([FromForm]AddRoleInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();

            if (string.IsNullOrEmpty(model.RoleName))
            {
                res.Msg = "角色名称不能为空";
                return new JsonResult(res);
            }

            //判断数据库有没有相同账号
            if (_roleInfoBll.IsHadRoleInfo(model.RoleName))
            {
                res.Msg = "当前添加角色已存在";
                return new JsonResult(res);
            }
            else
            {
                RoleInfo entity = new RoleInfo()
                {
                    ID = Guid.NewGuid().ToString(),
                    RoleName = model.RoleName,
                    Describe = model.Describe,
                    CreateTime = DateTime.Now,
                    IsDelete = false
                };

                bool isSuccess = _roleInfoBll.Add(entity);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Status = 1;
                    res.Msg = "添加成功";
                    return new JsonResult(res);
                }
                else
                {
                    return new JsonResult(res);
                }
            }
        }

        /// <summary>
        /// 更新角色赋值
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUpdateRoleInfo(string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取角色信息
            RoleInfo roleInfo = _roleInfoBll.GetEntity(roleInfoId);
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Datas = roleInfo;
            return new JsonResult(res);
        }


        /// <summary>
        /// 更新角色方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateRoleInfo([FromForm]UpdateRoleInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_roleInfoBll.UpdateRoleInfo(model.ID, model.RoleName, model.Describe))
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                res.Status = 1;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteRoleInfo([FromForm] string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_roleInfoBll.DeleteRoleInfo(roleInfoId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 软删除多个角色
        /// </summary>
        /// <param name="roleInfoIds"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteRoleInfos([FromForm] List<string> roleInfoIds)
        {
            CustomActionResult res = new CustomActionResult();

            //判断是否为管理员
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                return new JsonResult(res);
            }
            else
            {
                if (!userInfo.IsAdmin)
                {
                    res.Msg = "当前用户没有权限操作";
                    return new JsonResult(res);
                }
            }

            if (roleInfoIds == null || roleInfoIds.Count == 0)
            {
                res.Msg = "请选择要删除的数据";
            }
            else
            {
                _roleInfoBll.SoftDeleteEntities(roleInfoIds);
                res.Status = 1;
                res.Msg = "删除成功";
                res.IsSuccess = true;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 根据id获取角色信息
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleInfo(string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            RoleInfo roleInfo = _roleInfoBll.GetEntity(roleInfoId);
            if (roleInfo == null)
            {
                return new JsonResult(res);
            }
            else
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                res.Datas = roleInfo;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取用户备选数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBindUserInfo(string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //查询用户信息下拉选数据
            object userInfoOptions = _useInfoBll.GetUserInfoSelectOption();
            //获取当前角色已绑定的用户id集合
            List<string> userInfoIds = _roleInfoBll.GetBindUserIds(roleInfoId);
            res.Status = 1;
            res.IsSuccess = true;
            res.Datas = new
            {
                UserInfoOptions = userInfoOptions,
                UserInfoIds = userInfoIds
            };
            return new JsonResult(res);
        }

        /// <summary>
        /// 绑定角色方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BinUserInfo([FromForm] string roleInfoId, [FromForm]List<string> userInfoIds)
        {
            CustomActionResult res = new CustomActionResult();
            _roleInfoBll.BinUserInfo(roleInfoId, userInfoIds);
            res.Status = 1;
            res.IsSuccess = true;
            res.Msg = "绑定成功";
            return new JsonResult(res);
        }

        /// <summary>
        /// 获取菜单备选数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBindMenuInfo(string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //查询菜单信息下拉选数据
            object menuInfoOptions = _menuInfoBll.GetMenuInfoSelectOption();
            //获取当前角色已绑定的菜单id集合
            List<string> menuInfoIds = _roleInfoBll.GetBindMenuIds(roleInfoId);
            res.Status = 1;
            res.IsSuccess = true;
            res.Datas = new
            {
                MenuInfoOptions = menuInfoOptions,
                MenuInfoIds = menuInfoIds
            };
            return new JsonResult(res);
        }


        /// <summary>
        /// 绑定菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BinRoleInfo([FromForm] string roleInfoid, [FromForm] List<string> menuInfoid)
        {
            CustomActionResult res = new CustomActionResult();
            _roleInfoBll.BindMenuInfo(roleInfoid, menuInfoid);
            res.Status = 1;
            res.IsSuccess = true;
            res.Msg = "绑定成功";
            return new JsonResult(res);
        }
    }
}
